function varargout = TLSinkDlg(action,varargin)

% TL_SINK_DLG manages the user interface for TargetLink Sink block
%
% SYNTAX dlgInfo = TLSinkDlg('GetDlgInfo');
%   gets dialog info of TargetLink Sink block
%
%   INPUT ARGUMENTS
%   -/-      
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLSinkDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLSinkDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLSinkDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLSinkDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLSinkDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLSinkDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_SimBlocksUI/Sources/MSrc/project.pj $
% $Revision: 1.15 $
% $Date: 2011/07/21 15:04:51MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


switch action
    case 'GetDlgInfo'
        hBlock = varargin{1};
        dlgInfo = tl_get_dlginfodefaults('tlsim');
        titleName = 'Sink';
        pathinfo = tl_pathinfo(hBlock);
        dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
        dlgInfo.bEditCode   = 0;
        dlgInfo.bViewCode   = 0;
        dlgInfo.pageNames   = {'Logging', 'Documentation'};
        varargout{1} = dlgInfo;
        varargout{2} = get_tlcg_data(hBlock);

    case 'CtrlPositions'
        pageName     = varargin{1};
        commonGeom   = varargin{2};
        sheetDims    = varargin{4};
        switch pageName
            case 'Logging'
                [sheetDims, ctrlPos] = FcnCalcLoggingCtrlPositions(commonGeom, sheetDims);

            case 'Documentation'
                bTblBlock = 0;
                bCodeComment = 0;
                [sheetDims, ctrlPos] = TLManageDocCtrls( ...
                    'ctrlpos', commonGeom, sheetDims, bCodeComment, bTblBlock);
        end
        varargout{1} = sheetDims;
        varargout{2} = ctrlPos;

    case 'Create'
        pageName = varargin{1};
        dlgfig   = varargin{2};
        dlgdata  = varargin{3};
        pageNum  = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'Logging'
                dlgdata = ...
                    FcnCreateLoggingPage(pageNum, dlgfig, dlgdata);

            case 'Documentation'
                dlgdata = ...
                    TLManageDocCtrls('create', pageNum, dlgfig, dlgdata);
        end
        dlgdata.pageData{pageNum}.ctrlPos = [];
        varargout{1} = dlgdata;

    case 'Manage',
        pageName   = varargin{1};
        dlgfig     = varargin{2};
        dlgdata    = varargin{3};
        pageAction = varargin{4};
        pageNum    = strmatch(pageName, dlgdata.pageNames, 'exact');

        switch pageName
            case 'Logging'
                [dlgdata, bModified] = ...
                    FcnManageLoggingPage(pageNum, dlgdata, pageAction);

            case 'Documentation'
                [dlgdata,bModified] = TLManageDocCtrls('callback', ...
                    pageNum, dlgfig, dlgdata, pageAction);
        end
        varargout{1} = dlgdata;
        varargout{2} = bModified;

    case 'Update'
        dlgfig  = varargin{1};
        dlgdata = varargin{2};
        pageNum = varargin{3};
        if pageNum == 1
            % Logging page
            Children = dlgdata.pageData{1}.children;
            UserData = get(Children.stepsizeEdit, 'UserData');
            UserData.ctrlStatus.string = dlgdata.data.dt;
            UserData.ctrlStatus.enable = 'on';
            set(Children.stepsizeEdit, 'UserData', UserData);
            
            dlgdata =  TLManageLoggingCtrls('update',pageNum,dlgdata,'logdata', '');
            TLManageLoggingCtrls('setCtrls',pageNum, dlgfig, dlgdata);
            TLSetCtrlsStatus(dlgfig, Children, {'stepsizeEdit'});
        elseif pageNum == 2
            TLManageDocCtrls('update', pageNum, dlgfig, dlgdata);
        end
        varargout{1} = dlgdata;

    case 'Apply'
        dlgdata  = varargin{2};
        bClose   = varargin{3};
        [dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);
        varargout{1} = dlgdata;
        varargout{2} = bClose;

    otherwise
        error(['Invalid action "' action '"']);
end


%-----------------------------------------------------------------------------------------
% FcnCalcLoggingCtrlPositions calculates positions for logging controls 
%
% INPUT ARGUMENTS
%   commonGeom      common geometrie information
%   sheetDims       modified dimension of sheet
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims,ctrlPos] = FcnCalcLoggingCtrlPositions(commonGeom,sheetDims)

excludedCtrls = {'autoscaling', 'plotEdit'};
[sheetDims, ctrlPos] = ...
    TLManageLoggingCtrls('calcCtrls', commonGeom, sheetDims, excludedCtrls);

strings_and_styles = {...
    'Sample time:',  'text';
    '12345678901234','edit'};

[stepsizeLabelSize, ...
    stepsizeEditSize] = ...
    ds_get_uicontrol_sizes(commonGeom,strings_and_styles);

stepsizeEditSize(1) = ctrlPos.samplesLabel(1) - stepsizeLabelSize(1) - 25;

cx = commonGeom.figSideEdgeBuffer + commonGeom.sheetSideEdgeBuffer;
cy = ctrlPos.logplotGroupbox(2) ...
    + ctrlPos.logplotGroupbox(4) ...
    + commonGeom.textHeight;
stepsizeGroupboxWidth  = ctrlPos.logplotGroupbox(3);
stepsizeGroupboxHeight = commonGeom.editHeight + ...
    commonGeom.frameBottomEdgeBuffer + ...
    commonGeom.frameTopEdgeBuffer;
ctrlPos.stepsizeGroupbox = ...
    [cx cy stepsizeGroupboxWidth stepsizeGroupboxHeight];

cx = commonGeom.frameSideEdgeBuffer;
cy = commonGeom.frameBottomEdgeBuffer;
ctrlPos.stepsizeLabel = ...
    [cx cy stepsizeLabelSize];
cx = cx + stepsizeLabelSize(1) + 8;
ctrlPos.stepsizeEdit = ...
    [cx cy stepsizeEditSize];

width = stepsizeGroupboxWidth + ...
    2*commonGeom.frameSideEdgeBuffer;

height = sheetDims(2) + ...
    stepsizeGroupboxHeight + ...
    commonGeom.textHeight;

sheetDims = [width height];


%-----------------------------------------------------------------------------------------
% FcnCreateLoggingPage creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateLoggingPage(pageNum,dlgfig,dlgdata)


Children = dlgdata.pageData{pageNum}.children;
ctrlPos = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

% stepsize controls
Children.stepsizeGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',            {'Sample time'}, ...
    'Units',            'pixels', ...
    'Position',          ctrlPos.stepsizeGroupbox);

Children.stepsizeLabel = uicontrol( ...
    'Parent',             Children.stepsizeGroupbox, ...
    'Style',              'text', ...
    'String',             'Sample time:', ...
    'Position',           ctrlPos.stepsizeLabel);

Children.stepsizeEdit = uicontrol( ...
    'Parent',             Children.stepsizeGroupbox, ...
    'BackgroundColor',    'w', ...
    'Style',              'edit', ...
    'String',             dlgdata.data.dt, ...
    'Position',           ctrlPos.stepsizeEdit, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''stepsizeEdit'')',pageName));

dlgdata.pageData{pageNum}.children = Children;

% create logging controls
dlgdata = TLManageLoggingCtrls('create', pageNum, dlgfig, dlgdata);


%-----------------------------------------------------------------------------------------
% FcnManageLoggingPage manage control actions of logging page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata,bModified] = FcnManageLoggingPage(pageNum, dlgdata, action)

bModified = 0;
Children = dlgdata.pageData{pageNum}.children;

switch action
    case 'stepsizeEdit'
        stepsizestr = get(Children.stepsizeEdit,'String');
        if ~isempty(stepsizestr)
            dlgdata.data.dt = stepsizestr;
            bModified = 1;
        else
            beep;
        end

    otherwise
        [dlgdata, bModified] = ...
            TLManageLoggingCtrls('manage', pageNum, dlgdata, action, 'logdata');

end % switch action
